package com.litong.rms.Interceptor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.litong.rms.bean.Module;
import com.litong.rms.bean.User;
import com.litong.rms.util.AuthUtil;
import com.litong.rms.util.HttpUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String url = request.getRequestURI();
		String root = request.getContextPath();
		if(url.matches("^"+root+"/login.*")) {
			
			String result = invocation.invoke();
			User sysUser = (User)request.getSession().getAttribute("user");
			if (sysUser == null) {
				return "logout";
			}
			
			List<Module> moduleList = new ArrayList<Module>();
			moduleList = AuthUtil.getModuleList(sysUser);
			HttpUtil.setSession("moduleList", moduleList);
			return result;
		}
		
		User sysUser = (User)request.getSession().getAttribute("user");
		if (sysUser == null) {
			return "logout";
		}
		
		String userName = request.getParameter("user.name").trim();
		String password = request.getParameter("user.password").trim();
		System.out.println("URL:"+url+"   "+"root:"+root+"   "+"userName:"+userName+"   "+"password:"+password+"");
		return invocation.invoke();
	}


}
